define(function(require, exports){
	var $ = require('jquery');
	var pubjs = require('pubjs');
	var input  = require('@base/common/input');

	var listCache = null;
	// 暂时弃用，使用newZone
	var Country = input.subLevelCheckbox.extend({
		init: function(config, parent){
			config = pubjs.conf(config, {
				'target': parent
			});

			this.Super('init', arguments);
			if (listCache){
				this.setList(listCache);
			}else {
				//pubjs.data.get('/rest/listcountry', this);
				//pubjs.data.get(ROOT('config/country.json'), this);
				pubjs.mc.send('category/zone', {ChannelId: 168, Layer: 5}, this.onData.bind(this));
			}

		},
		afterBuild: function(){
			var self = this,
				el = self.getDOM(),
				buttonsDiv = $('<div class="M-commonSLCButtons"></div>');
			el.prepend(buttonsDiv);
			var buttons = [
				{'class': 'selectFirstLine', 'onFunc': 'eventSelectFirstLine', 'text': LANG('一线城市')},
				{'class': 'selectSecondLine', 'onFunc': 'eventSelectSecondLine', 'text': LANG('二线城市')},
				{'class': 'selectThirdLine', 'onFunc': 'eventSelectThirdLine', 'text': LANG('三线城市')},
				{'class': 'selectInverse', 'onFunc': 'eventSelectInverse', 'text': LANG('反选')},
				{'class': 'clearAll', 'onFunc': 'eventClearAll', 'text': LANG('清空')}
			]
			var i, len
			for(i = 0, len = buttons.length; i < len; i++) {
				var val = buttons[i];
				// 添加辅助按钮标签
				$('<a class="' + val['class'] + '">' + val['text']  + '</a>').appendTo(buttonsDiv);
				// 添加事件
				this.dg(el, '.' + val['class'], 'click', val['onFunc']);
			}

			// 特殊城市数据
			this.$cityData = {
				// 一线城市
				'firstLine': ["10761_10782_0", "10761_10783_0", "10761_10789_47667", "10761_10789_16496", "10761_10787_0"],
				// 二线城市
				'secondLine': ["10761_10787_69468", "10761_10765_37634", "10761_10773_104007", "10761_10779_103995",
					"10761_12768_104038", "10761_10786_33489", "10761_11573_36785", "10761_10763_68433", "10761_10785_33494",
					"10761_10779_104009", "10761_10763_54096", "10761_10785_59532", "10761_10769_104006", "10761_10766_25160",
					"10761_10768_104085", "10761_10770_104026", "10761_10772_104044", "10761_10768_104084", "10761_10774_104317",
					"10761_10788_104002", "10761_10776_104058", "10761_10765_33383", "10761_10765_104040", "10761_10764_104083",
					"10761_10778_104137", "10761_10777_104110", "10761_10762_104129", "10761_10784_104063", "10761_10771_232179",
					"10761_10780_104622", "10761_10789_57963", "10761_10789_59143", "10761_10771_104301", "10761_10785_104047",
					"10761_10768_104130", "10761_10780_234748"],
				// 三线城市
				'thirdLine': ["10761_10781_104211","10761_10767_104339","10761_10770_104113","10761_10765_104089",
					"10761_10765_104055","10761_10765_104013","10761_10785_104135","10761_10785_104191","10761_10763_104123",
					"10761_10763_104116","10761_10763_333580","10761_10769_239044","10761_10779_104062","10761_10789_232153",
					"10761_10789_104157","10761_10789_104057","10761_10766_104060","10761_10777_104069","10761_10775_104132",
					"10761_10771_104092","10761_10771_104250","10761_10771_104190","10761_10771_105534","10761_10785_104079",
					"10761_10785_104204","10761_10785_104374","10761_10785_104356","10761_10785_104303","10761_10785_104308",
					"10761_10785_104076","10761_10763_104121","10761_10763_104118","10761_10763_104117","10761_10765_104309",
					"10761_10765_104039","10761_10765_104144","10761_10765_104046","10761_10777_104056","10761_10789_104221",
					"10761_10789_104068","10761_10789_104220","10761_10789_104146","10761_10772_104136","10761_10772_104246",
					"10761_10772_104203","10761_12768_104532","10761_10773_104148","10761_10770_104042","10761_10770_104271",
					"10761_10770_104273","10761_10764_104143","10761_10764_104294","10761_10762_104128","10761_10786_24848",
					"10761_10769_104259","10761_10769_104257","10761_10779_104410","10761_10779_104073","10761_10779_104507",
					"10761_10779_104074","10761_10779_104077","10761_10779_104075","10761_10771_104344","10761_10771_104254",
					"10761_10771_104609","10761_10784_104516","10761_12768_104535","10761_10776_253019","10761_10774_104235",
					"10761_10770_104195","10761_10770_104253","10761_10770_104070","10761_10770_104091","10761_10770_104109",
					"10761_10785_104358","10761_10785_104142","10761_10785_104261","10761_10762_104156","10761_10762_104180",
					"10761_10762_104335","10761_10765_104188","10761_10765_234026","10761_10763_106316","10761_10763_104283",
					"10761_10773_104487","10761_10762_104177","10761_10764_104641","10761_10764_104587","10761_10772_104081",
					"10761_10772_104193","10761_10772_104262","10761_10768_104119","10761_10789_104403","10761_10789_333558",
					"10761_10789_104175","10761_10789_104214","10761_10777_104342","10761_10777_105302","10761_10786_104408",
					"10761_10786_104371","10761_10778_67317","10761_10788_104524"]
			}


		},
		onData: function(err, data){
			if (err){
				pubjs.error(err.message);
				return false;
			}
			listCache = data;
			this.setList(listCache);

			// 所有可选择的城市列表，用于反选
			var selectableCitys = [];
			// 此方法用于获得所有具体城市（不包括省份、地区）的列表
			// 参数为城市数据的顶层对象
			// 注意方法内会递归调用
			function findCity(citys) {
				var city = null;
				for(var i = 0, len = citys.length; i < len; i++) {
					city = citys[i];
					if (city.child && city.child.length > 0) {
						findCity(city.child);
					} else {
						// 没有child或child长度为零，该选项为可选择
						selectableCitys.push(city.value)
					}
				}
			}
			findCity(listCache);
			this.$selectableCitys = selectableCitys;

		},
		// 选择一线城市
		eventSelectFirstLine: function(ev, elm) {
			var newSelected = this._merageArray(this.$cityData.firstLine, this.getSelectedSubCity());
			this.setData(newSelected);
		},
		// 选择二线城市
		eventSelectSecondLine: function(ev, elm) {
			var newSelected = this._merageArray(this.$cityData.secondLine, this.getSelectedSubCity());
			this.setData(newSelected);
		},
		// 选择三线城市
		eventSelectThirdLine: function(ev, elm) {
			var newSelected = this._merageArray(this.$cityData.thirdLine, this.getSelectedSubCity());
			this.setData(newSelected);
		},
		// 反选
		eventSelectInverse: function(ev, elm) {
			// 得到没选择的城市
			var inverseCitys = _complementArray(this.$selectableCitys, this.getSelectedSubCity())
			this.setData(inverseCitys);
		},
		// 清空选择
		eventClearAll: function(ev, elm) {
			this.setData([]);
		},
		// 取得已经选择的城市（不包括省份和地区，包括其他）
		getSelectedSubCity: function() {
			var self = this;
			this.$selectedSubCity = [];
			this.doms.body.find('input:checkbox[checked="checked"]').each(function() {
				var el = $(this);
				var val = el.val();
				// “其他”选项使用的是数字0而非字符串，需先转换
				if (val === "0") {
					val = 0;
				}
				if (self.$selectableCitys.indexOf(val) >= 0) {
					self.$selectedSubCity.push(val)
				}
			})
			return this.$selectedSubCity;
		},
		// 此方法用于合并两个数组，但不添加重复的元素
		// 考虑将该方法添加到util
		_merageArray: function(arr1, arr2) {
			var newArr = arr1.slice(0);
			arr2.filter(function(e) {
				if (arr1.indexOf(e) < 0) {
					newArr.push(e)
				}
			})
			return newArr;
		}
	})

	exports.base = Country;
	// 该方法取得子数组的补集
	// 第一个参数为父数组，第二个参数为子数组
	// 考虑将该方法添加到util
	function _complementArray(fatherArray, subArray) {
		return fatherArray.filter(function(e) {
			return subArray.indexOf(e) < 0;
		})
	}
})
